package com.dayuanit.dy16.atm.boatm.task;

import com.dayuanit.dy16.atm.boatm.entity.Salary;
import com.dayuanit.dy16.atm.boatm.enums.SalaryStatus;
import com.dayuanit.dy16.atm.boatm.handler.ExcelHander;
import com.dayuanit.dy16.atm.boatm.mapper.SalaryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 从数据库中 加载状态为待入库的 信息
 */
@Component
public class SalarySaveTask extends BaseTask {

    @Autowired
    private SalaryMapper salaryMapper;

    @Autowired
    private ExcelHander excelHander;

    public SalarySaveTask() {
        System.out.println(">>>>>>>>> SalarySaveTask()");
    }

    @Scheduled(cron = "0/2 * * * * ?")
    @Override
    public void doTask() {
        processSaveSalaryToDB();
    }

    private void processSaveSalaryToDB() {
        System.out.println(">>>>>>> save salary");
        List<Salary> salaries = salaryMapper.listSalary(SalaryStatus.wait_save.getK());

        for (Salary salary : salaries) {
            String fileName = salary.getExcelFileName();
            try {
                excelHander.readExcel(fileName, salary.getId());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
